package com.hongning.railway.ui.dynamic_photo;

import android.app.Application;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableField;
import android.databinding.ObservableList;
import android.support.annotation.NonNull;

import com.hongning.railway.R;
import com.hongning.railway.data.AppRepository;
import com.hongning.railway.entity.reqbean.ReqDynamicBean;
import com.hongning.railway.entity.resbean.BaseRes;
import com.hongning.railway.entity.resbean.ResDynamicBean;
import com.hongning.railway.entity.resbean.ResPhotoItem;
import com.hongning.railway.ui.dynamic_photo.itemViewModel.ImageItemViewModel;
import com.hongning.railway.util.Apputils;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.BR;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.http.ApiDisposableObserver;
import me.goldze.mvvmhabit.http.BaseResponse;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class DynamicPhotoFragmentViewModel extends BaseViewModel<AppRepository> {
    public List<ResPhotoItem> photoItems = new ArrayList<>();
    public ObservableField<ResDynamicBean.DataBean> dynamicData = new ObservableField<>();

    public ItemBinding<ImageItemViewModel> item = ItemBinding.of(BR.viewModel, R.layout.item_image);
    public ObservableList<ImageItemViewModel> viewlist = new ObservableArrayList<>();
    public DynamicPhotoFragmentViewModel(@NonNull Application application, AppRepository model) {
        super(application, model);
    }

    public void getDynamicPhotos(String newsId){
        ReqDynamicBean reqDynamicBean = new  ReqDynamicBean();
        reqDynamicBean.setPhotoAlbumsId(newsId);
        reqDynamicBean.setUserId(Apputils.getUserId());
        reqDynamicBean.setPageNum(1);
        reqDynamicBean.setPageSize(200);
        model.getDynamicPhotos(reqDynamicBean)
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .compose(RxUtils.schedulersTransformer())
                .subscribe(new ApiDisposableObserver<BaseRes<List<ResPhotoItem>>>() {
                    @Override
                    public void onResult(BaseRes<List<ResPhotoItem>> listBaseRes) {
                        if (listBaseRes!=null){
                            photoItems = listBaseRes.getData();
                            for (ResPhotoItem item:listBaseRes.getData()) {
                                ImageItemViewModel imageItemViewModel = new ImageItemViewModel(DynamicPhotoFragmentViewModel.this,item);
                                viewlist.add(imageItemViewModel);
                            }
                        }
                    }
                });

    }
}
